package frsf.cidisi.exercise.modelo.util;

public class Laberinto implements Cloneable{

	private static int tam = 4;
	private Celda[][] celdas;

	public Laberinto() {
		celdas = new Celda[tam][tam];
	}
	
	public Laberinto(Integer x, Integer y) {
		celdas = new Celda[y][x];
	}
	
	public Laberinto(Celda[][] celdas) {
		super();
		this.celdas = celdas;
	}

	public Celda[][] getCeldas() {
		return celdas;
	}

	public void setCeldas(Celda[][] celdas) {
		this.celdas = celdas;
	}
	
	public Celda getCelda(Integer x, Integer y) {
		return this.celdas[y][x];
	}
	
	public Celda getCelda(Coordenada posicion) {
		return getCelda(posicion.getX(), posicion.getY());
	}
	
	public void addCelda(Coordenada posicion, Celda celda){
		int x = posicion.getX().intValue();
		int y = posicion.getY().intValue();
		
		this.celdas[y][x] = celda;
	}
	
	public int getSize() {
		return this.celdas.length;
	}
	
	@Override
	public String toString() {
		StringBuffer str = new StringBuffer();
		for(int i=0; i<celdas.length; i++) {
			for (int j=0; j<celdas[i].length; j++) {
				if(celdas[i][j] != null) {
					if(celdas[i][j].isEntrada()){
						str.append("E");
					}
					else if(celdas[i][j].isSalida() && celdas[i][j].isCerrada()){
						str.append("C");
					}
					else if(celdas[i][j].isSalida()){
						str.append("S");
					}
					else if(celdas[i][j].hasLlave()){
						str.append("L");
					}
					else {
						str.append("-");
					}
				}
			}
			str.append(System.getProperty("line.separator"));
		}
		return str.toString();
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException {

		Laberinto clone = (Laberinto) super.clone();

		// make the shallow copy of the object of type Department
		clone.celdas = celdas.clone();
		return clone;

	}
	
	@Override
	public boolean equals(Object obj) {
		
		if(! (obj instanceof Laberinto) )
			return false;
		
		Laberinto objLab = (Laberinto) obj;
		Celda[][] objCeldas = objLab.getCeldas();
		for(int i = 0; i < objCeldas.length; i++) {
			for(int j = 0; j < objCeldas[i].length; j++) {
				if(! (objCeldas[i][j].equals(this.celdas[i][j])) )
					return false;
			}
		}
		return true;
	}
}
